//
//  EditSelectedDishViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-2-4.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "EditSelectedDishViewController.h"
#import "MyOrderViewController.h"

#define TABLE_CELL_HEIGHT 54

@interface EditSelectedDishViewController ()
{
    int totalCount;
    float totalMoney;
}
@end

@implementation EditSelectedDishViewController

- (id)init
{
    self = [super initWithNibClass:[EditSelectedDishViewController class]];
    if (self) {
        _dataSource = [[EditSelectedDishDataSource alloc] initWithVC:self];
        _selectedDishList = [[NSMutableArray alloc] initWithCapacity:10];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"已点菜单"];
    
    self.selectedDishTable.delegate = self.dataSource;
    self.selectedDishTable.dataSource = self.dataSource;
    
    [self.selectedDishTable reloadData];
    [self.selectedDishTable autoHeight];
    
    [UIView autoLayoutVertical:@[_selectedDishTable, _totalView, _btnSubmit]];
    [UIView drawBorders:@[_selectedDishTable, _totalView]];
    
    [_theScrollview autoHeight];
    
    [self totalAction];
}

- (IBAction)leftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)selectDishCountFinished
{
    [self totalAction];
}

- (void)totalAction
{
    totalCount = 0;
    totalMoney = 0;
    
    for (NSDictionary *dic in _selectedDishList) {
        int count = [[dic objectForKey:@"fenshu"] intValue];
        float money = [[dic objectForKey:@"price"] floatValue];
        
        totalCount += count;
        totalMoney += money*count;
    }
    
    self.totalCountLabel.text = [NSString stringWithFormat:@"/%d道菜",totalCount];
    self.totalMoneyLabel.text = [NSString stringWithFormat:@"￥%.0f元",totalMoney];
}

#pragma 获取选中的菜品列表
- (NSString*)getDishString
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    for (NSDictionary *dic in _selectedDishList) {
        if ([[dic objectForKey:@"fenshu"] intValue] > 0) {
            NSString *cid = [dic objectForKey:@"cid"];
            if (!cid) cid = [dic objectForKey:@"id"];
            NSString *price = [dic objectForKey:@"price"];
            NSString *ccount = [dic objectForKey:@"fenshu"];
            
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:cid,@"cid",price,@"price",ccount,@"ccount", nil];
            [array addObject:dictionary];
        }
    }
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:array,@"caipin", nil];
    return [dictionary JSONString];
}

- (IBAction)doneOrderAction:(id)sender
{
    NSString *caipin = [self getDishString];
    NSString *url = [NSString stringWithFormat:API_EDIT_DISH_ORDER,_orderId,caipin];
    
    [_dataSource submitOrderWithURL:url];
}

@end

//======================================================================================
#pragma ===================数据源、数据请求等封装===========================================
//======================================================================================

#define ORDER_DISH_TAG 1002

@implementation EditSelectedDishDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
    }
    
    return self;
}

- (void)submitOrderWithURL:(NSString*)url
{
    [SVProgressHUD showWithStatus:@"订单修改正在提交中..."];
    [self get:url tag:ORDER_DISH_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    //state值为1时说明有座位，为2时没有座位数据
    CLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    NSDictionary *data = [json objectForKey:@"data"];
    int state = [[data objectForKey:@"state"] intValue];
    
    if (request.tag == ORDER_DISH_TAG) {
        if (state == 1) {
            [SVProgressHUD dismissWithSuccess:@"您的订单修改成功"];
            [_viewController pushOrPopViewController:[MyOrderViewController class]];
        }
        else if(state == 2) {
            [SVProgressHUD dismissWithError:@"您的订单修改失败"];
        }
    }
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewController.selectedDishList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellId = @"SelectedDishCell";
    SelectedDishCell *cell = [_viewController createCellWithNib:CellId tableview:tableView];
    
    NSMutableDictionary *dic = [self.viewController.selectedDishList objectAtIndex:indexPath.row];
    [cell setContent:dic];
    cell.delegate = self.viewController;
    
    return cell;
}

@end
